﻿Imports System.IO

Public Class FrmConsutarProd

    Private Sub FrmConsutarProd_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        pbImagen.SendToBack()
    End Sub

    Private Sub tbDescripcion_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbDescripcion.TextChanged

    End Sub

    Private Sub tbDescripcion_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbDescripcion.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            pbImagen.Image = Nothing
            cargarGvProdutos()
        End If
    End Sub

    Private Sub btBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btBuscar.Click
        pbImagen.Image = Nothing
        cargarGvProdutos()
    End Sub

    Private Sub cbImgProd_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles cbImgProd.CheckedChanged
        If cbImgProd.Checked Then
            gvProductos.Size = New Size(515, 408)
        Else
            gvProductos.Size = New Size(799, 408)
        End If
    End Sub

    Private Sub gvProductos_CurrentCellChanged(sender As System.Object, e As System.EventArgs) Handles gvProductos.CurrentCellChanged
        selectProd()
    End Sub

    Private Sub gvProductos_RowHeaderMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gvProductos.RowHeaderMouseClick
        selectProd()
    End Sub

    Private Sub cargarGvProdutos()
        gvProductos.DataSource = Nothing
        gvProductos.Rows.Clear()
        Try
            gvProductos.DataSource = GBDZafiro.Producto.ConsxDescripcion(tbDescripcion.Text)
            With gvProductos
                .Columns("PRECIO 1($)").DefaultCellStyle.Format = "N"
                .Columns("PRECIO 2($)").DefaultCellStyle.Format = "N"
                .Columns("PRECIO 3($)").DefaultCellStyle.Format = "N"
                .Columns("PRECIO1 + IVA($)").DefaultCellStyle.Format = "N"
                .Columns("PRECIO2 + IVA($)").DefaultCellStyle.Format = "N"
                .Columns("PRECIO3 + IVA($)").DefaultCellStyle.Format = "N"
                .Columns("PRECIO 1($)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Columns("PRECIO 2($)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Columns("PRECIO 3($)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Columns("PRECIO1 + IVA($)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Columns("PRECIO2 + IVA($)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Columns("PRECIO3 + IVA($)").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub selectProd()
        Try
            Dim codigo As String
            Dim rowgvUsuario As New DataGridViewRow()
            rowgvUsuario = gvProductos.CurrentRow
            codigo = rowgvUsuario.Cells.Item("CODIGO").Value.ToString()
            cargarImagen(codigo)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cargarImagen(nombre As String)
        Dim direccion As String = Environment.CurrentDirectory
        direccion += "\imgProducto\" + nombre + ".jpg"
        Try
            If File.Exists(direccion) Then
                Dim imagen As Image = Image.FromFile(direccion)
                Dim bmap As New Bitmap(imagen)
                imagen.Dispose()
                imagen = Nothing
                pbImagen.Image = bmap
            Else
                pbImagen.Image = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

End Class